package org.rc.web;

import java.io.IOException;

import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.rc.dao.GenericDao;
import org.rc.dao.ResultDao;
import org.rc.model.Athlete;
import org.rc.model.Result;

/**
 * Athlete servlet. Handle requests for /athlete.
 * 
 */
public class AthleteServlet extends IndexServlet {

    private GenericDao<Athlete> athleteDao = new GenericDao<Athlete>(Athlete.class);
    private ResultDao resultDao = new ResultDao();

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Long id = new Long(request.getParameter("id"));
        
        Athlete athlete = (Athlete) cache.get("athlete"+id);
        List<Result> results = (List<Result>) cache.get("athleteResults"+id);
        if(athlete==null || results==null) {
            athlete = athleteDao.get(id);
            results = resultDao.getResultsForAthlete(athlete);
            cache.put("athlete"+id, athlete);
            cache.put("athleteResults"+id,results);
        }
        request.setAttribute("results", results);
        request.setAttribute("athlete", athlete);
        forward(request, response, "athlete.jsp");
    }
}
